package com.lpc.android.abc.main.activity;

import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lpc.android.abc.R;
import com.lpc.android.abc.base.base.BaseActivity;
import com.lpc.android.abc.base.base.BaseApplication;
import com.lpc.android.abc.base.bean.Ware;
import com.lpc.android.abc.base.bean.WareDetails;
import com.lpc.android.abc.base.constants.Constants;
import com.lpc.android.abc.base.di.component.DaggerAppComponent;
import com.lpc.android.abc.base.widget.TopNavBar;
import com.lpc.android.abc.main.adapter.WaresListAdatper;
import com.lpc.android.abc.main.di.component.DaggerMainComponent;
import com.lpc.android.abc.main.di.module.MainModule;
import com.lpc.android.abc.main.mvp.MainContract;
import com.lpc.android.abc.main.mvp.MainPresenter;
import com.vlonjatg.progressactivity.ProgressActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by lipc0113 on 2017/6/29 0029.
 */

public class WareListActivity extends BaseActivity
        implements TabLayout.OnTabSelectedListener, MainContract.WareListView {

    @BindView(R.id.toolbar)
    TopNavBar toolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.txt_summary)
    TextView txtSummary;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.progressActivity)
    ProgressActivity progressActivity;

    public static final int TAG_DEFAULT = 0;
    public static final int TAG_SALE = 1;
    public static final int TAG_PRICE = 2;

    private int orderBy = 0;

    private long campaignId = 0;

    private int curPage = 1;

    private static final int pageSize = 10;

    @Inject
    MainPresenter mainPresenter;

    private WaresListAdatper mWaresAdapter;

    private List<WareDetails> mList = new ArrayList<>();

    @Override
    protected int setActivityLayoutId() {
        return R.layout.activity_warelist;
    }

    @Override
    protected void initData() {

//        DaggerAppComponent appComponent = DaggerAppComponent.builder();
        DaggerMainComponent.builder().appComponent(BaseApplication.getAppComponent())
                .mainModule(new MainModule()).build().inject(this);

//        mainPresenter = MainPresenter.getInstance();
        mainPresenter.setWareListView(this);

        campaignId = getIntent().getLongExtra(Constants.COMPAINGAIN_ID, 0);

        initToolBar();
        initTab();

        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {

                mainPresenter.getWaresList(getParams(true), true);
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {

                mainPresenter.getWaresList(getParams(false), false);
            }
        });

        mWaresAdapter = new WaresListAdatper(this, mList);
//            mWaresAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
//                @Override
//                public void onItemClick(View view, int position) {

//                    Wares wares = mWaresAdapter.getItem(position);
//                    Intent intent = new Intent(WareListActivity.this, WareDetailActivity.class);
//                    intent.putExtra(Contants.WARE, wares);
//                    startActivity(intent);
//                }
//            });
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.setAdapter(mWaresAdapter);

        refreshLayout.startRefresh();

    }

    @NonNull
    private Map<String, String> getParams(boolean isRefresh) {

        if(isRefresh){
            curPage = 1;
        }else{
            curPage++;
        }

        Map<String, String> maps = new HashMap<>();
        maps.put("campaignId", String.valueOf(campaignId));
        maps.put("orderBy", String.valueOf(orderBy));
        maps.put("curPage", String.valueOf(curPage));
        maps.put("pageSize", String.valueOf(pageSize));
        return maps;
    }

    private void initTab() {

        TabLayout.Tab tab = tabLayout.newTab();
        tab.setText("默认");
        tab.setTag(TAG_DEFAULT);
        tabLayout.addTab(tab);

        tab = tabLayout.newTab();
        tab.setText("价格");
        tab.setTag(TAG_PRICE);
        tabLayout.addTab(tab);

        tab = tabLayout.newTab();
        tab.setText("销量");
        tab.setTag(TAG_SALE);
        tabLayout.addTab(tab);

        tabLayout.addOnTabSelectedListener(this);
    }

    private void initToolBar() {

        toolbar.setRightButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WareListActivity.this.finish();
            }
        });

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        orderBy = (int) tab.getTag();
        refreshLayout.startRefresh();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainPresenter.setWareListView(null);
    }

    @Override
    public void waresListSuccess(Ware ware, boolean isRefresh) {

        if(isRefresh){
            mList.clear();
        }

        txtSummary.setText("共有" + ware.getTotalCount() + "件商品");

        mList.addAll(ware.getList());
        mWaresAdapter.notifyDataSetChanged();

        if(isRefresh){
            refreshLayout.finishRefreshing();
        }else{
            refreshLayout.finishLoadmore();
        }

    }

    @Override
    public void waresListFail(String fail, boolean isRefresh) {

        if(isRefresh){
            refreshLayout.finishRefreshing();
        }else{
            refreshLayout.finishLoadmore();
        }
        progressActivity.showError(getResources().getDrawable(R.drawable.load_fail),
                getString(R.string.prompt), getString(R.string.load_fail), getString(R.string.load_again),
                new ProgressActivity.OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        progressActivity.showLoading();
                        mainPresenter.getWaresList(getParams(true), true);
                    }
                });
    }
}
